<?php

    include ("../global_conf.php");
    require_once($site->classdir."/DBmanager.php");
    require_once("./admin_lib.php");
    require_once($site->langdir."/".$_SESSION['lang'].".lang.php");

    function get_data_section() {
        global $_LANG;
?>
<div class="post">
    <h2 class="title"><?php echo $_LANG['choose an action']; ?></h2>
    <a href="?k=1"><?php echo $_LANG['insert new datatype']; ?></a>
    <a href="?k=2"><?php echo $_LANG['manage datatypes attributes']; ?></a>
    <a href="?k=3"><?php echo $_LANG['insert new data']; ?></a>
    <a href="?k=4"><?php echo $_LANG['display inserted data']; ?></a>
</div>
<?php
    }

    function get_user_section() {
        global $_LANG;
?>
<div class="post">
    <h2 class="title"><?php echo $_LANG['choose an action']; ?></h2>
        <?php
            if (strcmp($_SESSION['role'], "all") == 0) {
                echo "<a class=\"datalink\" href=\"user.php?a=ins\">".$_LANG['insert new user']."</a>\n
                      <a class=\"datalink\" href=\"user.php?a=lst\">".$_LANG['list users']."</a>";
            }
            else
                echo $_LANG['no privileges to list users'];
        ?>
</div>
<?php
    }

    function get_account_section() {
        global $_LANG;
?>
<script type="text/javascript" language="javascript">
    function checkForm() {
        //var elems=new array();
        //elems[0]='change_username';
        //elems[1]='change_password';
        //elems[2]='repeat_password';
        /*for (elem in elems) {
            var e=document.getElementById(elem).value;
            if (e .value == null || e.length == 0) {
                alert('Campi incompleti');
                return false;
            }
        }*/
        //confirm("Vuoi veramente cambiare i tuoi dati?");
        return confirm('Vuoi cambiare veramente i tuoi dati?');
    }
</script>
<div class="post">
    <h2 class="title"><?php echo $_LANG['account options']; ?></h2>
<fieldset>
    <legend><?php echo $_LANG['change language']; ?></legend>
<form class="accountform" action="account.php" method="post">
    <select name="language">
        <?php
                global $languages;
                foreach ($languages as $lang => $langname) {
                    $str = "<option name=\"".$lang."\"";
                    $str.=$lang==$_SESSION['lang']?" selected=selected":"";
                    echo $str.">".$langname."</option>";
              }
        ?>
    </select>
        <input type="submit" name="change_lang" value="Ok" />
</form>
</fieldset>
<fieldset>
    <legend><?php echo $_LANG['change credentials'] ?></legend>
    <form id="accountform" class="accountform" action="account.php" method="post" onsubmit="return checkForm()">
        <label>Username</label>
        <input type="text" id="change_username" name="change_username" />
        <label>Password</label>
        <input type="password" id="change_password" name="change_password" />
        <label><?php echo $_LANG['repeat password']; ?></label>
        <input type="password" id="repeat_password" name="repeat_change_password" />
        <input type="hidden" name="usrid" value=<?php echo "\"".$_SESSION['logged']."\""; ?> />
        <input type="submit" name="submit_change_credentials" value="Ok" />
    </form>
</fieldset>
</div>
<?php
    }

    function get_insert_datatype_section() {
        global $_LANG;
?>
<div class="post">
    <h3 class="title"><?php echo $_LANG['insert the new datatype']; ?></h3>
    <fieldset>
    <legend>Content-type</legend>
    <form id="insertform" action="data.php" method="post">
        <label><?php echo $_LANG['complete name']; ?></label>
        <input type="text" name="fname" />
        <label><?php echo $_LANG['short name']; ?></label>
        <input type="text" name="sname" /><br /><br />
        <input type="submit" name="submit_datatype_name" value="<?php echo $_LANG['submit'] ?>" />
    </form>
    </fieldset>
</div>
<?php
    }

    function get_choose_data_section() {
        global $_LANG;
?>
<div class="post">
    <h2 class="title"><?php echo $_LANG['choose insert datatype']; ?></h2>
    <?php
        echo "<a class=\"datalink\" href=\"data.php?i=core\">Core</a>";
        $types=get_datatypes_names();
        foreach ($types as $type) {
            echo "<a class=\"datalink\" href=\"data.php?i=".$type['name']."\">".$type['complete_name']."</a>";
        }
    ?>
</div>
<?php
    }

    function get_display_data_section() {
        global $_LANG;
?>
<div class="post">
    <h3 class="title"><?php echo $_LANG['contents inserted']; ?></h3>
    <?php
        $result=get_table_values("core");
        if (!$result)
            echo "No elements in the table";
        else {
            $row = $result[0];
            unset($row['id']);
            $authors=get_usernames();
            $keys = array_keys($row);
            $table = "<table id=\"attrtab\"><tr>";
            foreach ($keys as $val)
                $table.="<td class=\"th\">".$_LANG[$val]."</td>";
            $table.="</tr>\n";
            $index=1;
            foreach ($result as $row) {
                $id=$row['id'];
                unset($row['id']);
                $table.="<tr";
                $table.=(++$index)%2 != 0?" class=\"odd\"":"";
                $table.=">";
                foreach ($row as $attr => $value) {
                    $table.="<td><a href=\"?view=".$id."\">";
                    if (strcmp($attr, "published") == 0) {
                        $value=$value?$_LANG['yes']:$_LANG['no'];
                    }
                    else if (strcmp($attr, "author") == 0) {
                        $value=$authors[$value];
                    }
                    $table.=shorten_string($value)."</a></td>";
                }
                $table.="</tr>\n";
            }
            $table.="</table>";
            if ($index == 1)
                $table="<div>No elements</div>";
            echo $table;
        }
    ?>
</div>
<?php
    }

    function get_insert_data_section($type, $data=null) {
        global $types, $_LANG;
?>
<div class="post">
    <h3 class="title"><?php echo $data?$_LANG['update content']:$_LANG['insert new content']; ?></h3>
    <fieldset>
    <legend><?php echo $_LANG['content']; ?></legend>
    <form enctype="multipart/form-data" id="insertform" action="data.php" method="post">
    <label><?php echo $_LANG['title']; ?></label>
    <input type="text" name="title" <?php if (isset($data)) echo "value=\"".$data['title']."\""; ?> />
    <label><?php echo $_LANG['description']; ?></label>
    <textarea cols="20" rows="5"  name="description"><?php if (isset($data)) echo $data['description']; ?></textarea>
    <?php
        if ($type != null && strcmp($type, "core")) {
            $attributes=get_attributes($type);
            $attributes_names=get_attributes_property("full_name");
            $attributes_types=get_attributes_property("type");
            $count=1;
            foreach ($attributes as $attr) {
                echo "<label>".$attributes_names[$attr['Field']]."</label>";
                $realtype=$attributes_types[$attr['Field']];
                switch ($realtype) {
                    case "string":
                    case "integer":
                    case "video":
                        $str="<input type=\"text\" name=\"".$attr['Field'].":".$count."\" ";
                        if (isset($data))
                            $str.="value=\"".$data[$attr['Field']]."\" ";
                        else if (strcmp($realtype, "video") == 0)
                                $str.="value=\"Paste youtube video here\" ";
                        echo $str."/>";
                        break;
                    case "date":
                        // spezzetto e passo i valori di default
                        if (isset($data))
                            $date=explode("-", $data[$attr['Field']]);
                        $str="<div class=\"spacer\">";
                        if (isset($date))
                                $str.=get_date_list($attr['Field'].":".$count.":day", 1, 31, $date[2])."\n".
                                        get_date_list($attr['Field'].":".$count.":month", 1, 12, $date[1])."\n".
                                        get_date_list($attr['Field'].":".$count.":year", 2010, 2050, $date[0]);
                        else
                            $str.=get_date_list($attr['Field'].":".$count.":day", 1, 31)."\n".
                                        get_date_list($attr['Field'].":".$count.":month", 1, 12)."\n".
                                        get_date_list($attr['Field'].":".$count.":year", 2010, 2050);
                        echo $str."</div>";
                        break;
                    case "image":
                        echo "<input type=\"file\" name=\"".$attr['Field'].":".$count."\" />";
                        break;
                    case "text":
                        $str="<textarea name=\"".$attr['Field'].":".$count."\">";
                        if (isset($data))
                            $str.=$data[$attr['Field']];
                        echo $str."</textarea>";
                        break;
                    default:
                        echo "tipo non supportato";
                }
                ++$count;
            }
            echo "<input type=\"hidden\" name=\"content_type\" value=\"".$type."\" />\n";
        }
        if (isset($data)) {
            echo "<input type=\"hidden\" name=\"content_id\" value=\"".$data['id']."\" />\n".
                "<input type=\"submit\" name=\"update_data\" value=\"Update\" />";
        }
        else
            echo "<input type=\"submit\" name=\"submit_data\" value=\"Insert\" />"
    ?>
    </form>
    </fieldset>
</div>
<?php
    }

    function get_add_attributes_section() {
        global $_LANG;
?>
<div class="post">
    <h2 class="title"><?php echo $_LANG['choose attribute datatype']; ?></h2>
    <?php
        $types=get_datatypes_names();
        if (empty($types) || count($types) < 1) {
            echo $_LANG['no datatype inserted yet'];
        }
        else {
            foreach ($types as $type) {
                echo "<a href=\"data.php?m=".$type['name']."\">".$type['complete_name']."</a>";
            }
        }
    ?>
</div>
<?php
    }

    function get_manage_attribute_section($table) {
        global $_LANG, $types;
?>
<div class="post">
    <h3 class="title"><?php echo $_LANG['insert new attribute']; ?></h3>
    <fieldset>
    <legend><?php echo $_LANG['properties']; ?></legend>
    <form id="insertform" action="data.php?m=<?php echo $table; ?>" method="post">
    <label><?php echo $_LANG['attribute full name']; ?></label>
    <input type="text" name="attr_full_name" />
    <label><?php echo $_LANG['attribute short name']; ?></label>
    <input type="text" name="attr_short_name" />
    <label><?php echo $_LANG['attribute type']; ?></label>
    <select name="attr_type">
        <?php
            foreach ($types as $tname => $tvalue) {
                echo "<option value=\"".$tname."\">".$tname."</option>";
            }
        ?>
    </select>
    <?php echo "<input type=\"hidden\" name =\"t_name\" value=\"content_type_".$table."\" />"; ?>
    <input type="submit" name="add_attribute" value="Ok" />
    </form>
    </fieldset>
</div>
<!-- Parte sulla gestione degli attributi -->
<div class="post">
    <h3 class="title"><?php echo $_LANG['reorder attributes']; ?></h3>
    <p><?php echo $_LANG['drag up and down attributes']; ?></p>
    <fieldset>
        <legend><?php echo $_LANG['attributes']; ?></legend>
        <?php
            $attributes=get_attributes($table);
            if (!$attributes || count($attributes) < 1)
                echo "<div class=\"entry\"><strong><em>".$_LANG['datatype has no attributes']."</em></strong></div>";
            else {
echo <<<EOT
                <noscript>
                    Riordino senza javascript non ancora implementato. Attiva javascript o usa un browser piu` recente
                </noscript>
                <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
                <script type="text/javascript" src="js/jquery-ui-1.8.5.custom.min.js"></script>
                <style type="text/css">
                    #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
                    #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
                    #sortable li span { position: absolute; margin-left: -1.3em; }
                    #sortable li:hover { cursor: move; }
                </style>
                <script type="text/javascript" lang="javascript">
                    function getOrder() {
                        return $("#sortable").sortable("toArray");
                    }

                    $(function() {
                        $("#sortable").sortable({
                                        placeholder: 'ui-state-highlight'
                        });
                        $("#sortable").disableSelection();
                    });
                </script>
                <form action="data.php?m=$table" method="post" onsubmit="document.getElementById('orderfield').value=getOrder();">
                <ul id="sortable">
EOT;
                $attr_names=get_attributes_property("full_name");
                foreach ($attributes as $attr) {
                    echo "<li class=\"ui-state-default\" id=\"".$attr['Field']."\">".$attr_names[$attr['Field']]."</li>";
                }
                echo "</ul>\n".
                     "<input type=\"hidden\" id=\"orderfield\" name=\"order\" value=\"\" />\n".
                     "<input type=\"hidden\" name=\"content_type\" value=\"$table\" />\n".
                     "<input type=\"submit\" name=\"submit_reorder\" value=\"Ok\" />\n".
                     "</form>";
            }
        ?>
    </fieldset>
</div>
<?php
    }

    function get_insert_user_section() {
        global $_LANG, $languages;
?>
<div class="post">
    <h2 class="title"><?php echo $_LANG['new user']; ?></h2>
    <fieldset>
        <legend><?php echo $_LANG['insert new user']; ?></legend>
        <form id="insertform" action="user.php?" method="post">
            <label>Username</label>
            <input type="text" name="usr" />
            <label>Password</label>
            <input type="password" name="passwd" />
            <label><?php echo $_LANG['role']; ?></label>
            <select name="role">
                <option value="insert"><?php echo $_LANG['role insert']; ?></option>
                <option value="publish"><?php echo $_LANG['role publish']; ?></option>
                <option value="all"><?php echo $_LANG['role all']; ?></option>
            </select>
            <label><?php echo $_LANG['preferred lang']; ?></label>
            <select name="preferred_lang">
                <?php
                    foreach ($languages as $key => $value)
                        echo "<option value=\"".$key."\">".$value."</option>";
                ?>
            </select>
            <input type="submit" name="submit_new_user" value="Ok" />
        </form>
    </fieldset>
</div>
<?php
    }

    function get_list_user_section() {
        global $_LANG;
?>
<div class="post">
    <h2 class="title"><?php echo $_LANG['user list']; ?></h2>
    <?php
        $users=get_table_values("user");
        $keys=array_keys($users[0]);
        unset($keys[0]);
        $table="<table id=\"attrtab\">";
        foreach ($keys as $key => $value) {
            $table.="<td class=\"th\">".$value."</td>";
        }
        $table.="</tr>\n";
        $index=1;
        foreach ($users as $row) {
            $id=$row['id'];
            unset($row['id']);
            $table.="<tr";
            $table.=(++$index)%2 != 0?" class=\"odd\"":"";
            $table.=">";
            foreach ($row as $key => $value) {
                $table.="<td>".$value."</td>";
            }
            $table.="</tr>\n";
        }
        $table.="</table>";
        echo $table;
    ?>
</div>
<?php
    }
?>